package com.fone.player.view;

import android.content.Context;
import android.content.res.Configuration;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.activity.main.MainTabActivity;
import com.fone.player.activity.main.SplashActivity;
import com.fone.player.client.LoginRst;
import com.fone.player.client.Reporter;
import com.fone.player.constant.FoneConstant;
import com.fone.player.entity.Download;
import com.fone.player.play.FonePlayerActivity;
import com.fone.player.storage.SharedPreferenceModule;
import com.fone.player.storage.StorageModule;
import com.fone.player.util.ScreenUtil;
import com.fone.player.util.SystemUtil;

@SuppressWarnings("unused")
public class UpdateDialog implements OnClickListener {

	private static final String TAG = "UpdateDialog";

	public static final int SUGGEST = 0;
	public static final int FORCE = 1;

	private TextView mUpdateDesc;
	private CustomDialog dialog;
	private Context mContext;
	private int updateFlag;
	private LoginRst loginRst;
	private String updateDesc;

	public UpdateDialog(Context mContext, MainTabActivity activity,
			String updateDesc, int updateFlag, LoginRst loginRst) {
		this.updateDesc = updateDesc;
		this.mContext = mContext;
		this.updateFlag = updateFlag;
		this.loginRst = loginRst;
		View updateView = LayoutInflater.from(mContext).inflate(
				R.layout.personal_update_dialog, null);
		dialog = new CustomDialog((Context) activity, updateView);
		initDialogView(mContext, updateView);

	}

	public UpdateDialog(Context mContext, FonePlayerActivity activity,
			String updateDesc, int updateFlag, LoginRst loginRst) {
		this.updateDesc = updateDesc;
		this.mContext = mContext;
		this.updateFlag = updateFlag;
		this.loginRst = loginRst;
		View updateView = LayoutInflater.from(mContext).inflate(
				R.layout.personal_update_dialog, null);
		dialog = new CustomDialog((Context) activity, updateView);
		initDialogView(mContext, updateView);

	}

	public UpdateDialog(Context mContext, SplashActivity activity,
			String updateDesc, int updateFlag, LoginRst loginRst) {
		this.updateDesc = updateDesc;
		this.mContext = mContext;
		this.updateFlag = updateFlag;
		this.loginRst = loginRst;
		View updateView = LayoutInflater.from(mContext).inflate(
				R.layout.personal_update_dialog, null);
		dialog = new CustomDialog((Context) activity, updateView);
		initDialogView(mContext, updateView);

	}

	private void initDialogView(Context mContext, View updateView) {
		// 上报弹窗
		Reporter.logEvent(Reporter.EventId.UPGRADE_POPUP);
		// 记录该次升级时间
		SharedPreferenceModule.getInstance().setLong(
				FoneConstant.LAST_UPDATA_TIME_SP, System.currentTimeMillis());
		
		if(loginRst.softupdate!=null && "1".equals(loginRst.softupdate.updateflag)){
			dialog.setCanceledOnTouchOutside(false);
			dialog.setCancelable(false);
		}else{
			dialog.setCanceledOnTouchOutside(true);
			dialog.setCancelable(true);
		}

		dialog.show();
		// 记录升级框显示标志
		FoneConstant.isUpdate = true;
		if (getScreenOrientation() == Configuration.ORIENTATION_PORTRAIT) {
			updateView.setMinimumWidth((int) (ScreenUtil
					.getScreenWidthPix(mContext) * 0.8));// 设置dialog的宽度
		} else {
			updateView.setMinimumWidth((int) (ScreenUtil
					.getScreenWidthPix(mContext) * 0.5));// 设置dialog的宽度
		}

		mUpdateDesc = (TextView) updateView
				.findViewById(R.id.update_content_tv);
		mUpdateDesc.setText(updateDesc);
		Button changeSureBtn = (Button) updateView
				.findViewById(R.id.update_sure_id);
		changeSureBtn.setOnClickListener(this);

		Button changeCancelBtn = (Button) updateView
				.findViewById(R.id.update_cancel_id);
		changeCancelBtn.setOnClickListener(this);
		
		View btnPartingLine = updateView.findViewById(R.id.btn_parting_line);
		
		
		if(loginRst!=null&&loginRst.softupdate!=null
				&&loginRst.softupdate.btn!=null){
			changeSureBtn.setText(loginRst.softupdate.btn.btnjumplabel);
			changeCancelBtn.setText(loginRst.softupdate.btn.btncancellabel);
			
			switch (loginRst.softupdate.btn.btns) {
			case 0:
			case 2:
				if("1".equals(loginRst.softupdate.updateflag)){//如果为强制升级
					changeCancelBtn.setVisibility(View.GONE);
					btnPartingLine.setVisibility(View.GONE);
				}
				break;
			case 1: //仅有确定按钮
				changeCancelBtn.setVisibility(View.GONE);
				btnPartingLine.setVisibility(View.GONE);
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.update_sure_id:
			Reporter.logEvent(Reporter.EventId.UPGRADE_CLICK_OK);
			if (!TextUtils.isEmpty(loginRst.softupdate.updateurl)) {
				// 记录当前升级时的版本号,为升级成功上报做铺垫
				SharedPreferenceModule.getInstance().setString(
						FoneConstant.UPDATE_VERSION,
						SystemUtil.getAppVersion(mContext));

				Download download = StorageModule.getInstance().getDownload(
						loginRst.softupdate.updateurl);

				if (download != null) {
					if (download.getDownloadStateType() == Download.DOWNLOAD_STATE_FINISH) {
						StorageModule.getInstance().installApp(
								download.getDownloadStoragePath(),
								download.getDownloadUrl());
					} else {
						download.setDownloadIsShowRunningNotification(true);
						download.setDownloadNotification(mContext);
						download.setDownloadIsInstall(true);
						download.setDownloadIsErrorToast(true);
						StorageModule.getInstance()
								.updateFileDownload(download);
					}
				} else {
					download = new Download();
					download.setDownloadFileName(loginRst.softupdate.name);
					download.setDownloadIsShowRunningNotification(true);
					download.setDownloadNotification(mContext);
					download.setDownloadIsInstall(true);
					download.setDownloadIsLimitSpeed(false);
					download.setDownloadIsErrorToast(true);
					download.setDownloadUrl(loginRst.softupdate.updateurl);
					download.setDownloadType(Download.DOWNLOAD_UPGRADE);
					StorageModule.getInstance().addFileDownload(download);
				}
			}
			dialog.cancel();
			break;
		case R.id.update_cancel_id:
			Reporter.logEvent(Reporter.EventId.UPGRADE_CLICK_CANCEL);
			if (FoneConstant.FORCE_UPDATE
					.equals(loginRst.softupdate.updateflag)) {
				// FoneUtil.showToast(mContext, "未完成,强制升级退出应用");
				ApplicationManage.getAplicationManageInstance().exitSystem();
			}
			dialog.cancel();
			break;
		}
	}

	/**
	 * 获取屏幕方向
	 * 
	 * @return
	 * @return int
	 * @throws
	 */
	private int getScreenOrientation() {
		return mContext.getResources().getConfiguration().orientation;
	}
}
